package org.jxc.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.jxc.dao.PurchaseOrderProductDAO;
import org.jxc.domain.PurchaseOrder;
import org.jxc.domain.PurchaseOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.dao.DAOFactory;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.Arithmetic;
import org.team4.util.Validate;

@Service
public class PurchaseOrderProductService implements IService {

	private PurchaseOrderProductDAO purchaseOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		purchaseOrderProductDAO = (PurchaseOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, PurchaseOrderProductDAO.class);
	}

	/**
	 * 根据订单ID获取隶属于其的采购订单明细
	 * 
	 * @return
	 * @throws CRUDException
	 */
	public List<PurchaseOrderProduct> findAllPurchaseOrderProductByPurchaseOrderId(
			String purchaseOrderId) throws CRUDException {
		List<PurchaseOrderProduct> list = purchaseOrderProductDAO.find(
				"from PurchaseOrderProduct where purchaseOrder.id = ?",
				purchaseOrderId);
		return list;
	}

	/**
	 * 根据id查询采购订单明细
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public PurchaseOrderProduct findById(String id) throws Exception {
		PurchaseOrderProduct purchaseOrderProduct = purchaseOrderProductDAO
				.get(id);
		return purchaseOrderProduct;
	}

	/**
	 * 新建采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void savePurchaseOrderProduct(
			PurchaseOrderProduct purchaseOrderProduct) throws CRUDException {
		purchaseOrderProductDAO.save(purchaseOrderProduct);
	}

	/**
	 * 修改采购订单明细
	 * 
	 * @param purchaseOrder
	 * @throws CRUDException
	 */
	public void updatePurchaseOrderProduct(
			PurchaseOrderProduct purchaseOrderProduct) throws CRUDException {
		PurchaseOrderProduct oldPurchaseOrderProduct = purchaseOrderProductDAO
				.get(purchaseOrderProduct.getId());
		oldPurchaseOrderProduct.setAmounts(purchaseOrderProduct.getAmounts());
		oldPurchaseOrderProduct.setDiscount(purchaseOrderProduct.getDiscount());
		oldPurchaseOrderProduct.setLastPrice(purchaseOrderProduct
				.getLastPrice());
		oldPurchaseOrderProduct.setPrice(purchaseOrderProduct.getPrice());
		oldPurchaseOrderProduct.setProduct(purchaseOrderProduct.getProduct());
		oldPurchaseOrderProduct.setPurchaseOrder(purchaseOrderProduct
				.getPurchaseOrder());
		oldPurchaseOrderProduct.setPurchasePrice(purchaseOrderProduct
				.getPurchasePrice());
		oldPurchaseOrderProduct.setRemark(purchaseOrderProduct.getRemark());
		oldPurchaseOrderProduct.setTax(purchaseOrderProduct.getTax());
		purchaseOrderProductDAO.update(oldPurchaseOrderProduct);
	}

	public void deleteProductOrderProductByProductOrderId(String purchaseOrderId)
			throws CRUDException {
		List<PurchaseOrderProduct> list = findAllPurchaseOrderProductByPurchaseOrderId(purchaseOrderId);
		for (PurchaseOrderProduct pop : list) {
			purchaseOrderProductDAO.delete(pop);
		}
	}

	/**
	 * 批量删除商品
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public List<List<String>> deleteProductOrderProduct(List<String> idList)
			throws CRUDException {
		if (Validate.collectionNotNull(idList)) {
			// 查找可删和不可删的集合
			List<List<String>> returnList = purchaseOrderProductDAO
					.findCannotDeleteList(idList,
							SQLConstant.DEPARTMENT_DELETE_LIST);
			// 如果都可以删除则执行删除操作
			if (returnList.get(1).size() == 0) {
				purchaseOrderProductDAO.deleteByIdList(idList);
				return null;
			}
			// 否则返回可删和不可删的双集合
			else
				return returnList;
		} else
			return null;
	}

}